package com.tl.app.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tl.app.domain.AppAccount;
import com.tl.app.domain.AppClerk;
import com.tl.app.domain.AppOrderPriceConfig;
import com.tl.app.mapper.AppAccountMapper;
import com.tl.app.mapper.AppClerkMapper;
import com.tl.app.mapper.AppClerkServerMapper;
import com.tl.app.mapper.AppTagClerkMapper;
import com.tl.app.service.IAppClerkService;
import com.tl.app.service.IAppOrderPriceConfigService;
import com.tl.common.enums.ClerkStatus;
import com.tl.common.utils.CodeUtil;
import com.tl.common.utils.DateUtils;

/**
 * 店员Service业务层处理
 * 
 * @author suhp
 * @date 2024-07-07
 */
@Service
public class AppClerkServiceImpl implements IAppClerkService 
{
    @Autowired
    private AppClerkMapper appClerkMapper;
    
    @Autowired
    private AppTagClerkMapper appTagClerkMapper;
    
    @Autowired
    private AppClerkServerMapper appClerkServerMapper;
    
    @Autowired
    private AppAccountMapper appAccountMapper;
    
    @Value("${tl.defaultClerkLevel}")
    private String defaultClerkLevel;
    

    @Autowired
    private IAppOrderPriceConfigService appOrderPriceConfigService;

    /**
     * 查询店员
     * 
     * @param userId 店员主键
     * @return 店员
     */
    @Override
    public AppClerk selectAppClerkByUserId(Long userId)
    {
        return appClerkMapper.selectAppClerkByUserId(userId);
    }

    /**
     * 查询店员列表
     * 
     * @param appClerk 店员
     * @return 店员
     */
    @Override
    public List<AppClerk> selectAppClerkList(AppClerk appClerk)
    {
        return appClerkMapper.selectAppClerkList(appClerk);
    }

    /**
     * 新增店员
     * 
     * @param appClerk 店员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAppClerk(AppClerk appClerk)
    {
        appClerk.setCreateTime(DateUtils.getNowDate());
        int i = appClerkMapper.insertAppClerk(appClerk);
        if(appClerk.getAtcList()!=null) {
        	appClerk.getAtcList().forEach(key->{
            	key.setClerkId(appClerk.getUserId());
            	key.setCreateTime(DateUtils.getNowDate());
            });
            appTagClerkMapper.batchAppTagClerk(appClerk.getAtcList());
        }
        return i;
    }

    /**
     * 修改店员
     * 
     * @param appClerk 店员
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAppClerk(AppClerk appClerk)
    {
    	appTagClerkMapper.deleteAppTagClerkByClerkId(appClerk.getUserId());
    	appClerkServerMapper.deleteAppClerkServerByClerkId(appClerk.getUserId());
    	if(appClerk.getAtcList()!=null&&appClerk.getAtcList().size()>0) {
	    	appClerk.getAtcList().forEach(key->{
	        	key.setClerkId(appClerk.getUserId());
	        	key.setCreateTime(DateUtils.getNowDate());
	        });
	    	appTagClerkMapper.batchAppTagClerk(appClerk.getAtcList());
    	}
    	AppClerk acOld=appClerkMapper.selectAppClerkByUserId(appClerk.getUserId());
    	if(appClerk.getAcsList()!=null&&appClerk.getAcsList().size()>0) {
	    	appClerk.getAcsList().forEach(key->{
	    		if(StringUtils.isNotBlank(acOld.getLevel())&&StringUtils.isEmpty(key.getYt())) {
	    			AppOrderPriceConfig aopc=new AppOrderPriceConfig();
		    		aopc.setConfigLevel(acOld.getLevel());
		    		aopc.setConfigServer(key.getServerCode());
		    		List<AppOrderPriceConfig> aopcs=appOrderPriceConfigService.selectAppOrderPriceConfigList(aopc);
		    		for(AppOrderPriceConfig a:aopcs) {
		    			String spec=a.getConfigSpec();
		    			String price=a.getPrice();
		    			switch(spec) {
							case "bxs":
								key.setBxs(price);
								break;
							case "yxs":
								key.setYxs(price);
								break;
							case "yt":
								key.setYt(price);
								break;
							case "yz":
								key.setYz(price);
								break;
							case "yy":
								key.setYy(price);
								break;
							default:
								break;
						}
		    		}
	    		}
	    		key.setClerkId(appClerk.getUserId());
	        	key.setCreateTime(DateUtils.getNowDate());
	    	});
	    	appClerkServerMapper.batchAppClerkServer(appClerk.getAcsList());
    	}
    	if(appClerk.getStatus().equals("wtg")) {
    		appClerk.setStatus("dsh");
    	}
        appClerk.setUpdateTime(DateUtils.getNowDate());
        return appClerkMapper.updateAppClerk(appClerk);
    }
    
    /**
     * 室内审核店员
     */
    @Transactional
    public int checkAppClerk(AppClerk appClerk)
    {
    	if(appClerk.getStatus().equals(ClerkStatus.YTG.getCode())) {
    		//如果通过，同步开通店员账户
    		AppAccount aaOld=appAccountMapper.selectAppAccountById(appClerk.getUserId());
    		if(aaOld==null) {
    			AppAccount aa=new AppAccount();
        		aa.setLb("gr");
        		aa.setId(appClerk.getUserId());
        		aa.setCreateTime(DateUtils.getNowDate());
        		aa.setCreateBy(appClerk.getUserId().toString());
        		appAccountMapper.insertAppAccount(aa);
    		}
    		String code=CodeUtil.getAppUserCode("DY");
    		appClerk.setCode(code);
    		appClerk.setLevel(defaultClerkLevel);
    	}
    	appClerk.setUpdateTime(DateUtils.getNowDate());
    	return appClerkMapper.updateAppClerk(appClerk);
    }

    /**
     * 批量删除店员
     * 
     * @param userIds 需要删除的店员主键
     * @return 结果
     */
    @Override
    public int deleteAppClerkByUserIds(Long[] userIds)
    {
        return appClerkMapper.deleteAppClerkByUserIds(userIds);
    }

    /**
     * 删除店员信息
     * 
     * @param userId 店员主键
     * @return 结果
     */
    @Override
    public int deleteAppClerkByUserId(Long userId)
    {
        return appClerkMapper.deleteAppClerkByUserId(userId);
    }

	@Override
	public List<AppClerk> selectAppClerkTop(AppClerk appClerk) {
		return appClerkMapper.selectAppClerkTop(appClerk);
	}
}
